﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Configuration;

namespace CrystalReportsManagementSystem
{
    public partial class CrystalReportsView : System.Web.UI.Page
    {
        ReportDocument Report = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            string ReportFile = Request["ReportFile"];
            Report = new ReportDocument();
            ConnectionInfo connectionInfo = new ConnectionInfo();
            connectionInfo.ServerName = ConfigurationManager.AppSettings["Server"];
            connectionInfo.DatabaseName = ConfigurationManager.AppSettings["Database"];
            connectionInfo.UserID = ConfigurationManager.AppSettings["UserId"];
            connectionInfo.Password = ConfigurationManager.AppSettings["Password"];
            string ReportName = Server.MapPath("ReportTemplate//" + ReportFile + ".rpt");
            Report.Load(ReportName);
            this.CrystalReportViewer1.ReportSource = Report;
            SetDBLogonForReport(connectionInfo);
        }
        private void SetDBLogonForReport(ConnectionInfo connectionInfo)
        {
            TableLogOnInfos tableLogOnInfos = this.CrystalReportViewer1.LogOnInfo;
            foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
            {
                tableLogOnInfo.ConnectionInfo = connectionInfo;
            }
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            //System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Report);
            if (Report != null)
            {
                Report.Close();
                Report.Dispose();
                GC.Collect();
            }
        }
    }
}